﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using Mvvm.Models;
using Mvvm.Services;
using ReactiveUI;

namespace Mvvm.ViewModels
{
    public class SentimentAnalysisViewModel : PageViewModelBase
    {
        private readonly ChatDataStorage _chatDataStorage;
        private readonly EmotionReportService _emotionReportService;
        private readonly IEmotionTrajectoryService _emotionTrajectoryService;

        public SentimentAnalysisViewModel()
        {
            _chatDataStorage = new ChatDataStorage();
            _emotionReportService = new EmotionReportService(new ChatDataProcessedStorage());
            _emotionTrajectoryService = new EmotionTrajectoryService();
            IsLoading = true;
            // 自动调用分析情感的方法，加载情感报告相关内容
            _ = AnalyzeTextAsync();
            // 自动调用显示情感轨迹的方法，加载情感轨迹相关图表数据
            _ = ShowEmotionTrajectoryAsync();
        }

        private bool _isLoading;
        public bool IsLoading
        {
            get => _isLoading;
            set { this.RaiseAndSetIfChanged(ref _isLoading, value); }
        }

        private string _message;
        public string Message
        {
            get => _message;
            set { this.RaiseAndSetIfChanged(ref _message, value); }
        }

        // 新增属性用于存储聊天复盘内容，将其与建议内容分开处理和展示
        private string _chatReview;
        public string ChatReview
        {
            get => _chatReview;
            set { this.RaiseAndSetIfChanged(ref _chatReview, value); }
        }

        // 用于存储情感轨迹折线图相关数据
        private ISeries[] _emotionTrajectorySeries;
        public ISeries[] EmotionTrajectorySeries
        {
            get => _emotionTrajectorySeries;
            set { this.RaiseAndSetIfChanged(ref _emotionTrajectorySeries, value); }
        }

        private Axis[] _emotionTrajectoryXAxes;
        public Axis[] EmotionTrajectoryXAxes
        {
            get => _emotionTrajectoryXAxes;
            set { this.RaiseAndSetIfChanged(ref _emotionTrajectoryXAxes, value); }
        }

        private Axis[] _emotionTrajectoryYAxes;
        public Axis[] EmotionTrajectoryYAxes
        {
            get => _emotionTrajectoryYAxes;
            set { this.RaiseAndSetIfChanged(ref _emotionTrajectoryYAxes, value); }
        }

        // 新增属性用于存储情感占比饼状图相关数据系列
        private ISeries[] _emotionPieSeries;
        public ISeries[] EmotionPieSeries
        {
            get => _emotionPieSeries;
            set { this.RaiseAndSetIfChanged(ref _emotionPieSeries, value); }
        }

        // 新增属性用于存储情绪占比饼状图相关数据系列
        private ISeries[] _emotionTonePieSeries;
        public ISeries[] EmotionTonePieSeries
        {
            get => _emotionTonePieSeries;
            set { this.RaiseAndSetIfChanged(ref _emotionTonePieSeries, value); }
        }

        public async Task AnalyzeTextAsync()
        {
            await _chatDataStorage.InitializeAsync();
            IsLoading = true;

            // 从数据库获取聊天数据列表
            var chatDataList = await _chatDataStorage.ListAsync();
            if (chatDataList.Any())
            {
                // 调用EmotionReportService进行情感数据统计
                _emotionReportService.AnalyzeEmotionData(chatDataList);
                // 生成情感报告
                _chatReview = _emotionReportService.GenerateEmotionReport();
                // 获取大模型建议
                string aiSuggestion = await _emotionReportService.GetSuggestionsFromAI(chatDataList);

                // 组合要显示在界面上的消息内容，先展示聊天复盘，再展示建议
                Message = _chatReview + "\n" + aiSuggestion;
            }
            else
            {
                Message = "数据库中无聊天数据可供分析";
            }
            IsLoading = false;
        }

        public async Task ShowEmotionTrajectoryAsync()
        {
            await _chatDataStorage.InitializeAsync();

            var chatDataList = await _chatDataStorage.ListAsync();
            if (chatDataList.Any())
            {
                var (series, xAxes, yAxes, emotionPieSeries, emotionTonePieSeries) = await _emotionTrajectoryService.GenerateEmotionTrajectoryData(chatDataList);
                EmotionTrajectorySeries = series;
                EmotionTrajectoryXAxes = xAxes;
                EmotionTrajectoryYAxes = yAxes;
                EmotionPieSeries = emotionPieSeries;
                EmotionTonePieSeries = emotionTonePieSeries;
            }
        }

        public override bool CanNavigateNext
        {
            get => false;
            protected set => throw new NotSupportedException();
        }

        // You cannot go back from this page
        public override bool CanNavigatePrevious
        {
            get => true;
            protected set => throw new NotSupportedException();
        }
    }
}